package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.bean.bo.CreateGroupReq;
import com.nl.controller.web.bean.bo.UpdateGroupInfoReq;
import com.nl.controller.web.bean.po.mgrgrouppo.SmsMgrGroupInfo;
import com.nl.controller.web.service.SmsMgrGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@Slf4j
@RequestMapping("smsMgrGroup")
/**
 * @desc 项目分组
 * @return null
 * @author l
 * @date 2021/9/13 16:53
 */
public class SmsMgrGroupController {
    @Autowired
    private SmsMgrGroupService smsMgrGroupService;

    /**
     * @desc 查询项目分组列表
     * @return com.nl.controller.common.model.Response
     * @author l
     * @date 2021/9/23 15:02
     */
    @RequestMapping("qryGroupList")
    public Response qryGroupList(@RequestParam(value = "page", required = true) int page,
                                 @RequestParam(value = "limit", required = true) int limit,
                                 @RequestParam(value = "cityId",required = false) String cityId,
                                 @RequestParam(value = "countyId",required = false) String countyId,
                                 @RequestParam(value = "groupName",required = false) String groupName,
                                 @RequestParam Map<String, Object> paramMap) {

            log.info(" 查询项目分组列表：{}", paramMap.toString());
            PageInfo<SmsMgrGroupInfo> pageInfo = new PageInfo<>();
            if ( cityId == null) {
                throw new SmsException("请选择查询条件，开始查询");
            } else{
                pageInfo = smsMgrGroupService.qryGroupList(page,limit,cityId,countyId,groupName);
            }

            log.info("查询项目分组列表返回参数：{}", pageInfo.getList().toString());
            return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @RequestMapping("qryGroupList2")
    public Response qryGroupList2(
                                 @RequestParam(value = "cityId",required = false) String cityId,
                                 @RequestParam(value = "countyId",required = false) String countyId,
                                 @RequestParam Map<String, Object> paramMap) {

        log.info(" 查询项目分组列表：{}", paramMap.toString());
        Response response = smsMgrGroupService.qryGroupList2(cityId,countyId);
        log.info("查询项目分组列表返回参数：{}", response.toString());
        return response;
    }

    /**
     * @desc 根据分组编码查询分组信息
     * @param groupId
     * @param paramMap
     * @return com.nl.controller.common.model.Response
     * @author l
     * @date 2021/10/7 20:46
     */
    @RequestMapping("qryGroupInfoById")
    public Response qryGroupInfoById(
                                 @RequestParam(value = "groupId",required = false) String groupId,
                                 @RequestParam Map<String, Object> paramMap) {
        log.info(" 查询项目分组列表：{}", paramMap.toString());
        Response response = smsMgrGroupService.qryGroupInfoById(groupId);
        log.info("查询项目分组列表返回参数：{}", response.toString());
        return response;
    }

    @RequestMapping("createGroupInfo")
    public Response createGroupInfo(@RequestBody @Validated CreateGroupReq createGroupReq, BindingResult result){
        if (result.hasErrors()) {
            throw new SmsException(result.getFieldError().getDefaultMessage());
        }
        String relMgrString = createGroupReq.getRelMgrString();
        if(StringUtils.isNotBlank(relMgrString)){
            String[] mgrArray = relMgrString.split("\\+");
            List mgrList = Arrays.asList(mgrArray);
            createGroupReq.setRelMgrList(mgrList);
        }
        log.info("新增项目分组信息请求参数：{}", createGroupReq.toString());
        Response response = smsMgrGroupService.createGroupInfo(createGroupReq);
        log.info("新增项目分组信息返回参数：{}", response.toString());
        return response;
    }

    @RequestMapping("updateGroupInfo")
    public Response updateGroupInfo(@RequestBody @Validated UpdateGroupInfoReq updateGroupInfoReq, BindingResult result){
        if (result.hasErrors()) {
            throw new SmsException(result.getFieldError().getDefaultMessage());
        }
        String relMgrString = updateGroupInfoReq.getRelMgrString();
        if(StringUtils.isNotBlank(relMgrString)){
            String[] mgrArray = relMgrString.split("\\+");
            List mgrList = Arrays.asList(mgrArray);
            updateGroupInfoReq.setRelMgrList(mgrList);
        }
        log.info("变更项目分组信息请求参数：{}", updateGroupInfoReq.toString());
        Response response = smsMgrGroupService.updateGroupInfo(updateGroupInfoReq);
        log.info("变更项目分组信息返回参数：{}", response.toString());
        return response;
    }

    @RequestMapping("deleteMgrGroup")
    public Response deleteMgrGroup(
            @RequestParam(value = "groupId",required = false) String groupId,
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap) {
        log.info(" 删除项目分组：{}", paramMap.toString());
        SmsUsers user = (SmsUsers) request.getSession().getAttribute("user");
        String operatorId = user.getUserId();
        Response response = smsMgrGroupService.deleteMgrGroup(groupId,operatorId);
        log.info("删除项目分组列表返回参数：{}", response.toString());
        return response;
    }

    @RequestMapping("judgeGroupNameRepeat")
    public Response judgeGroupNameRepeat(
            @RequestParam(value = "groupName",required = false) String groupName,
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap) {
        log.info(" 判断分组名称是否重复：{}", paramMap.toString());
        Response response = smsMgrGroupService.judgeGroupNameRepeat(groupName);
        log.info("判断分组名称是否重复返回参数：{}", response.toString());
        return response;
    }


}
